package com.spark.lola.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.CarInfo;
import com.spark.lola.logistics.mapper.CarInfoMapper;
import com.spark.lola.logistics.service.CarInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/24 13:08
 * 车辆信息业务实现
 **/
@Service
public class CarInfoServiceImpl extends BaseServiceImpl<CarInfoMapper, CarInfo> implements CarInfoService {

    @Autowired
    private CarInfoMapper carInfoMapper;

    @Override
    public IPage<CarInfo> findPage(QueryParam<CarInfo> queryParam) {
        PageParam<CarInfo> pageParam = queryParam.toPageParam();
        return carInfoMapper.findPage(pageParam.getPage(), pageParam.getEntityWrapper());
    }

    @Override
    public int findIsBind(long id) {
        return carInfoMapper.findIsBindById(id);
    }

    @Override
    public int updateIsBind(int isBind, long id) {
        return carInfoMapper.updateIsBindById(isBind, id);
    }

    @Override
    public List<SelectMap> selectMap(String key, Integer isBind) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,no");
        queryWrapper.eq("used", CommonConstant.Y);
        if (isBind != null) {
            queryWrapper.eq("is_bind", isBind);
        }
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(o -> o.like("no", key));
        }
        List<CarInfo> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps = new ArrayList<>();
        if (!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId(), item.getNo()));
            });
        }
        return selectMaps;
    }
}
